﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Diagnostics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;



namespace WindowsGameInput
{
	public class SoundsHandler
	{
		public enum SoundType {FIRE, EXPLOSION, WIN, DEFEAT}
		private static SoundEffectInstance fire, explosion, explosion2, win, defeat;


		public SoundsHandler()
		{
        }



		public static void Init(Game game)
		{
			fire = game.Content.Load<SoundEffect>("Sounds\\Fire2").CreateInstance();
			fire.Pitch += 0.42f;
			explosion = game.Content.Load<SoundEffect>("Sounds\\Explosion").CreateInstance();
			explosion2 = game.Content.Load<SoundEffect>("Sounds\\Explosion").CreateInstance();
			win = game.Content.Load<SoundEffect>("Sounds\\Win").CreateInstance();
			defeat = game.Content.Load<SoundEffect>("Sounds\\Defeat").CreateInstance();
		}



		public static void SetVolume(uint volume)
		{
			Debug.Assert(volume <= 10);
			float currentVolume = volume * 0.1f;

			fire.Volume = currentVolume;
			explosion.Volume = currentVolume;
			explosion2.Volume = currentVolume;
			win.Volume = currentVolume;
			defeat.Volume = currentVolume;
		}



		public static uint GetVolume()
		{
			float tmpVolume = fire.Volume * 10.0f;
			return (uint)tmpVolume;
		}



		public static void PlaySound(SoundType soundType)
		{
			switch(soundType)
			{
				case SoundType.FIRE:
					if(fire.State != SoundState.Playing)
						fire.Play();
					break;

				case SoundType.EXPLOSION:
					if(explosion.State == SoundState.Playing)
					{
						explosion.Stop();
						explosion2.Play();
					}
					else
						explosion.Play();
					break;

				case SoundType.WIN:
					if(win.State != SoundState.Playing)
						win.Play();
					break;

				case SoundType.DEFEAT:
					if(defeat.State != SoundState.Playing)
						defeat.Play();
					break;
			}
		}
	}
}
